//
//  GWNewsViewController.m
//  无限滚动Demo
//
//  Created by qianfeng on 16/3/1.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWNewsViewController.h"
#import "MJExtension.h"
#import "GWNews.h"
#import "GWNewsCell.h"
#define GWMyCollectionCellIdentifier @"GWMyCollectionCellIdentifier"
#define GWMaxSections 3
@interface GWNewsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,copy)NSArray *newsArray;
@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageController;

@end

@implementation GWNewsViewController
-(NSArray *)newsArray
{
    if (_newsArray ==nil) {
        _newsArray =[GWNews mj_objectArrayWithFilename:@"newses.plist"];
    }
    return _newsArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"GWNewsCell" bundle:nil] forCellWithReuseIdentifier:GWMyCollectionCellIdentifier];
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:GWMaxSections/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    self.pageController.currentPage =0;
    [self addNSTimer];
}
-(void)addNSTimer
{
    NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    self.timer =timer;
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}

-(NSIndexPath *)resetPageIndexPath
{
    NSIndexPath *indexPath =[[self.collectionView indexPathsForVisibleItems]lastObject];
    NSIndexPath *resetIndexPath =[NSIndexPath indexPathForItem:indexPath.item inSection:GWMaxSections/2];
    [self.collectionView scrollToItemAtIndexPath:resetIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    return resetIndexPath;
}
-(void)nextPage
{
    NSIndexPath *resetIndexPath =[self resetPageIndexPath];
    
    NSInteger item =resetIndexPath.item+1;
    NSInteger section =resetIndexPath.section;
    if (item>=self.newsArray.count) {
        item=0;
        section++;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:section] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    self.pageController.currentPage =item;
    
}
-(void)removeTimer
{
    [self.timer invalidate];
    self.timer =nil;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return GWMaxSections;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.newsArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GWNews *news =self.newsArray[indexPath.item];
    GWNewsCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:GWMyCollectionCellIdentifier forIndexPath:indexPath];
    cell.news =news;
    return cell;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addNSTimer];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageController.currentPage =(int)(self.collectionView.contentOffset.x/self.collectionView.bounds.size.width+0.5)%self.newsArray.count;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   [self resetPageIndexPath];
}
@end
